package com.tungse.study.itreator_mode;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
import java.util.function.Consumer;

/**
 * @author Tungse
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Iterable<String>{

    private String name;
    private int age;

    @Override
    public Iterator<String> iterator() {
        return new UserIterator();
    }


    class UserIterator implements Iterator<String> {
        int count = 2;
        @Override
        public boolean hasNext() {
            return count > 0;
        }

        @Override
        public String next() {
            count--;
            if(count == 1) {
                return User.this.name;
            }
            if(count == 0) {
                return User.this.age+"";
            }
            throw new NoSuchElementException();
        }
    }
}
